package com.zzzy.doc.controller.exchange;

import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.common.vo.PageResult;
import com.zzzy.doc.dao.entity.DocSendRecord;
import com.zzzy.doc.dao.entity.Document;
import com.zzzy.doc.service.DocService;
import com.zzzy.doc.service.exchange.DocReceiveRecordService;
import com.zzzy.doc.service.exchange.DocSendRecordService;
import com.zzzy.doc.vo.document.DocBasicInfoRes;
import com.zzzy.doc.vo.document.DocInfoRes;
import com.zzzy.doc.vo.document.exchange.receive.DocExchangeReceiveQueryRes;
import com.zzzy.doc.vo.document.exchange.send.DocExchangeSendPageReq;
import com.zzzy.doc.vo.document.exchange.send.DocExchangeSendQueryRes;
import com.zzzy.doc.vo.pageoffice.OpenOfficeFileRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Tag(name = "公文交换-单位发文", description = "doc200")
@Validated
@RestController
@RequestMapping("/doc/exchange/send")
public class DocExchangeSendController {

    @Resource
    private DocSendRecordService docSendRecordService;
    @Resource
    private DocReceiveRecordService docReceiveRecordService;
    @Resource
    private DocService docService;

    @Operation(summary = "单位发文分页列表")
    @GetMapping(value = "/page")
    @PreAuthorize("@pvs.hasPermission('doc:exchange:send:page')")
    public AjaxResult<PageResult<DocExchangeSendQueryRes>> page(@Valid @ParameterObject DocExchangeSendPageReq queryReq) {
        PageResult<DocExchangeSendQueryRes> docSendPage = docSendRecordService.getPage(queryReq);
        return AjaxResult.success(docSendPage);
    }

    @Operation(summary = "查询指定发文记录的收文记录")
    @GetMapping(value = "/getReceiveListByDocId/{sendId}")
    @Parameter(name = "sendId", description = "发文记录ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:exchange:send:page')")
    public AjaxResult<List<DocExchangeReceiveQueryRes>> getReceiveListByDocId(@NotNull(message = "发文记录ID不能为空") @PathVariable("sendId") Long sendId) {
        List<DocExchangeReceiveQueryRes> docReceiveList = docReceiveRecordService.getReceiveListBySendId(sendId);
        return AjaxResult.success(docReceiveList);
    }

    @GetMapping("/getRecord/{id}")
    @Operation(summary = "获取发文记录信息")
    @Parameter(name = "id", description = "发文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:send:page')")
    public AjaxResult<DocSendRecord> getRecord(@NotNull(message = "发文记录ID不能为空") @PathVariable("id") Long id) {
        DocSendRecord sendRecord = docSendRecordService.getByIdForQuery(id);
        return AjaxResult.success(sendRecord);
    }

    @GetMapping("/getDocBasicInfo/{id}")
    @Operation(summary = "获取发文记录中公文基本信息")
    @Parameter(name = "id", description = "发文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:send:page')")
    public AjaxResult<DocBasicInfoRes> getDocBasicInfo(@NotNull(message = "发文记录ID不能为空") @PathVariable("id") Long id) {
        DocBasicInfoRes doc = docSendRecordService.getDocBasicInfo(id);
        return AjaxResult.success(doc);
    }

    @GetMapping("/getDocImage/{id}")
    @Operation(summary = "获取公文正文第一页缩略图")
    @Parameter(name = "id", description = "发文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:send:page')")
    public void getDocImage(@NotNull(message = "发文记录ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocSendRecord sendRecord = docSendRecordService.getByIdForQuery(id);
        Document doc = docService.getDocById(sendRecord.getDocId());
        FileUtil.outputFileStream(response, doc.getDocThumbnailBlob(), doc.getSubject());
    }

    @GetMapping(value = "/getFinalPageOfficeFile/{id}")
    @Operation(summary = "打开最终文件（合并后的pdf）")
    @Parameter(name = "id", description = "发文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:send:page')")
    public AjaxResult<OpenOfficeFileRes> getFinalPageOfficeFile(@NotNull(message = "发文记录ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/doc/exchange/send/getFinalDocFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = docSendRecordService.getFinalPageOfficeResByDocId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getFinalDocFileStream/{id}")
    @Operation(summary = "获取最终文件流（合并后的pdf）")
    @Parameter(name = "id", description = "发文记录ID", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:exchange:send:page')")
    public void getFinalDocFileStream(@NotNull(message = "发文记录ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        DocInfoRes docInfo = docSendRecordService.getDocInfo(id);
        byte[] fileBlob = docInfo.getPdfFileBlob();
        FileUtil.outputFileStream(response, fileBlob, docInfo.getSubject() + "." + FileUtil.PDF);
    }

}
